package com.smart.village.big.screen.controller.micro;

import com.google.common.collect.Lists;
import com.smart.village.big.screen.domain.AjaxResult;
import com.smart.village.big.screen.domain.PartyMemberLogStaticVo;
import com.smart.village.big.screen.service.PartyFeesService;
import com.smart.village.big.screen.service.PartyOrganizationActivityService;
import com.smart.village.big.screen.service.PartyOrganizationApplicationService;
import com.smart.village.big.screen.service.PartyOrganizationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import static com.smart.village.big.screen.domain.AjaxResult.error;
import static com.smart.village.big.screen.domain.AjaxResult.success;

/**
 * 小程序-党组织情况
 */
@Tag(name = "小程序-党组织情况接口-管理员视角")
@RestController
@RequestMapping("/micro/manager/party/organization")
public class MicroPartyOrganizationManagerController {
    Logger logger = Logger.getLogger(MicroPartyOrganizationManagerController.class.getName());

    @Autowired
    private PartyOrganizationActivityService partyOrganizationActivityService;
    @Autowired
    private PartyOrganizationApplicationService partyOrganizationApplicationService;
    @Autowired
    private PartyFeesService partyFeesService;
    @Autowired
    private PartyOrganizationService partyOrganizationService;


    /**
     * 小程序 党员事务 党员管理
     * @param partyOrganizationSerialNumber
     * @param search
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Operation(summary = "小程序 党员事务 党员管理")
    @Parameters({
            @Parameter(name = "partyOrganizationSerialNumber", description = "党组id"),
            @Parameter(name = "search", description = "搜索"),
            @Parameter(name = "pageNum", description = "当前页数"),
            @Parameter(name = "pageSize", description = "每页条数")
    })
    @GetMapping("/getMicroManagerPartyMemberList")
    @ResponseBody
    public AjaxResult getMicroManagerPartyMemberList(
            @RequestParam String partyOrganizationSerialNumber,
            @RequestParam(required = false) String search,
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize

    ) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(partyOrganizationService.getMicroManagerPartyMemberList(partyOrganizationSerialNumber, search, pageNum, pageSize));
        }catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }

    }

    /**
     * 小程序 党员事务 党员管理 党员详情
     * @param iDCardNumber
     * @return
     */
    @Operation(summary = "小程序 党员事务 党员管理 党员详情")
    @Parameters({
            @Parameter(name = "iDCardNumber", description = "身份证号"),
    })
    @GetMapping("/getMicroManagerPartyMemberInformation")
    @ResponseBody
    public AjaxResult getMicroManagerPartyMemberInformation(
            @RequestParam String iDCardNumber
    ) {
        try {
            return success(partyOrganizationService.getMicroPartyMemberInformation(iDCardNumber));
        }catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }

    }


    /**
     * 小程序 党员事务 党费收缴
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 党费收缴")
    @Parameters({
            @Parameter(name = "year", description = ""),
            @Parameter(name = "partyOrganizationSerialNumber", description = "党组id")
    })
    @GetMapping("getMicroManagerPartyFees")
    @ResponseBody
    public AjaxResult getMicroManagerPartyFees(
            @RequestParam String year,
            @RequestParam String partyOrganizationSerialNumber
    ) {
        try {
            HashMap param = new HashMap();
            param.put("year", year);
            param.put("partyOrganizationSerialNumber", partyOrganizationSerialNumber);
            return success(partyFeesService.getMicroManagerPartyFees(param));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党员事务 党费收缴 缴纳情况 列表
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 党费收缴 缴纳情况 列表")
    @Parameters({
            @Parameter(name = "year", description = ""),
            @Parameter(name = "partyOrganizationSerialNumber", description = "党组id"),
    })
    @GetMapping("getMicroManagerPartyFeesDetail")
    @ResponseBody
    public AjaxResult getMicroManagerPartyFeesDetail(
            @RequestParam String year,
            @RequestParam String partyOrganizationSerialNumber
    ) {
        try {
            HashMap param = new HashMap();
            param.put("year", year);
            param.put("partyOrganizationSerialNumber", partyOrganizationSerialNumber);
            return success(partyFeesService.getMicroManagerPartyFeesDetail(param));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党员事务 党费收缴 缴纳情况 更多
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 党费收缴 缴纳情况 更多")
    @Parameters({
            @Parameter(name = "yearMonth", description = ""),
            @Parameter(name = "partyOrganizationSerialNumber", description = "党组id"),
    })
    @GetMapping("getMicroManagerPartyFeesDetailMore")
    @ResponseBody
    public AjaxResult getMicroManagerPartyFeesDetailMore(
            @RequestParam String yearMonth,
            @RequestParam String partyOrganizationSerialNumber,
            @RequestParam String status,
            @RequestParam(required = false) String search,
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        try {
            HashMap param = new HashMap();
            param.put("yearMonth", yearMonth);
            param.put("partyOrganizationSerialNumber", partyOrganizationSerialNumber);
            param.put("status", status);
            param.put("search", search);
            param.put("pageNum", pageNum);
            param.put("pageSize", pageSize);
            return success(partyFeesService.getMicroManagerPartyFeesDetailMore(param));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党员事务 党员发展
     * @param partyOrganizationSerialNumber
     * @param search
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Operation(summary = "小程序 党员事务 党员发展")
    @Parameters({
            @Parameter(name = "partyOrganizationSerialNumber", description = "党组id"),
            @Parameter(name = "search", description = "搜索"),
            @Parameter(name = "pageNum", description = "当前页数"),
            @Parameter(name = "pageSize", description = "每页条数")
    })
    @GetMapping("getMicroManagerPartyMemberDevelop")
    @ResponseBody
    public AjaxResult getMicroManagerPartyMemberDevelop(
            @RequestParam String partyOrganizationSerialNumber,
            @RequestParam(required = false) String search,
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
      try {
          if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
              pageNum = 1;
          }
          if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
              pageSize = 10;
          }
          return success(partyOrganizationService.getMicroManagerPartyMemberDevelop(partyOrganizationSerialNumber, search, pageNum, pageSize));
      }catch (Exception e) {
          e.printStackTrace();
          logger.severe(e.getMessage());
          return error("程序错误请联系系统管理员");
      }

    }


    /**
     * 小程序 党员事务 党员发展 入党材料接口
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 党员发展 入党材料接口")
    @Parameters({
            @Parameter(name = "applicationSerialNumber", description = "申请id")
    })
    @GetMapping("getMicroManagerPartyMemberLogStaticDetail")
    @ResponseBody
    public AjaxResult getMicroManagerPartyMemberLogStaticDetail(
            @RequestParam String applicationSerialNumber) {
        try {
            List<PartyMemberLogStaticVo> resultJson = partyOrganizationApplicationService.getMicroManagerPartyMemberLogStaticDetail(applicationSerialNumber);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党员事务 驻村工作队 列表
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 驻村工作队 列表")
    @Parameters({
            @Parameter(name = "partyOrganizationSerialNumber", description = "党组id"),
            @Parameter(name = "search", description = "搜索"),
            @Parameter(name = "pageNum", description = "当前页数"),
            @Parameter(name = "pageSize", description = "每页条数")
    })
    @GetMapping("getMicroManagerWorkTeamsList")
    @ResponseBody
    public AjaxResult getMicroManagerWorkTeamsList(
            @RequestParam String partyOrganizationSerialNumber,
            @RequestParam(required = false) String search,
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(partyOrganizationActivityService
                    .getMicroManagerWorkTeamsList(
                            partyOrganizationSerialNumber,search,pageNum,pageSize)
            );
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党员事务 驻村工作队 详情
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 驻村工作队 详情")
    @Parameters({
            @Parameter(name = "workTeamSerialNumber", description = "工作队id"),
    })
    @GetMapping("getMicroManagerWorkTeamsDetail")
    @ResponseBody
    public AjaxResult getMicroManagerWorkTeamsDetail(
            @RequestParam String workTeamSerialNumber) {
        try {
            return success(partyOrganizationActivityService
                    .getMicroManagerWorkTeamsDetail(
                            workTeamSerialNumber)
            );
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党员事务 换届补选
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 换届补选")
    @Parameters({
            @Parameter(name = "partyOrganizationSerialNumber", description = "选择的党组织"),
    })
    @GetMapping("getMicroManagerPartyOrganizationTenure")
    @ResponseBody
    public AjaxResult getMicroManagerPartyOrganizationTenure(
            @RequestParam String partyOrganizationSerialNumber) {
        try {
            return success(partyOrganizationService
                    .getMicroManagerPartyOrganizationTenure(
                            partyOrganizationSerialNumber)
            );
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党员事务 党员留言
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 党员留言")
    @GetMapping("getPartyMemberComments")
    @ResponseBody
    public AjaxResult getPartyMemberComments(
            @RequestParam @Parameter(name = "partyOrganizationSerialNumber", description = "选择的党组织") String partyOrganizationSerialNumber,
            @RequestParam(required = false, defaultValue = "") @Parameter(description = "回复状态") String replyStatus,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        {
            try {
                if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                    pageNum = 1;
                }
                if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                    pageSize = 10;
                }

                if (StringUtils.isNotEmpty(replyStatus)) {
                    if (replyStatus.equals("0")) {
                        replyStatus = "'提交','处理'";
                    }
                    if (replyStatus.equals("1")) {
                        replyStatus = "'已处理','拒绝'";
                    }
                }
                return success(
                        partyOrganizationService.
                                getPartyMemberComplaints(partyOrganizationSerialNumber,replyStatus, pageNum, pageSize));
            } catch (Exception e) {
                e.printStackTrace();
                logger.severe(e.getMessage());
                return error("程序错误请联系系统管理员");
            }
        }
    }

    /**
     * 小程序 党员事务 党员留言 - 详情
     *
     * @return
     */
    @Operation(summary = "小程序 党员事务 党员留言 - 详情")
    @GetMapping("getPartyMemberCommentsDetail")
    @ResponseBody
    public AjaxResult getPartyMemberCommentsDetail(
            @RequestParam @Parameter(name = "complaintId", description = "留言id") String complaintId
         ) {
        {
            try {
                return success(
                        partyOrganizationService.
                                getPartyMemberCommentsDetail(complaintId));
            } catch (Exception e) {
                e.printStackTrace();
                logger.severe(e.getMessage());
                return error("程序错误请联系系统管理员");
            }
        }
    }

    /**
     * 小程序  个人中心 账户设置
     * @param openld
     * @return
     */
    @Operation(summary = "小程序  个人中心 账户设置")
    @Parameters({
            @Parameter(name = "openld", description = "openld"),
    })
    @GetMapping("/getMicroAccountInformation")
    @ResponseBody
    public AjaxResult getMicroAccountInformation(
            @RequestParam String openld
    ){
        try {
            return success(partyOrganizationService.getMicroAccountInformation(openld));
        }catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }

    }


    /**
     * 小程序 党建事务 党建活动
     *
     * @return
     */
    @Operation(summary = "小程序 党建事务 党建活动")
    @Parameters({
            @Parameter(name = "partyOrganizationSerialNumber", description = "下拉选择的最底级的党组织"),
            @Parameter(name = "activityStatus", description = "活动状态"),
            @Parameter(name = "activityType", description = "活动类别"),
            @Parameter(name = "search", description = "搜索"),
            @Parameter(name = "phone", description = "用户id"),
            @Parameter(name = "pageNum", description = "当前页数"),
            @Parameter(name = "pageSize", description = "每页条数")
    })
    @GetMapping("/activity/getMicroManagerPartyOrganizationActivityList")
    @ResponseBody
    public AjaxResult getMicroManagerPartyOrganizationActivityList(
            @RequestParam String partyOrganizationSerialNumber,
            @RequestParam(required = false) String activityStatus,
            @RequestParam(required = false) String activityType,
            @RequestParam(required = false) String search,
            @RequestParam String phone,
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        try {
            List<String> activityTypeList = Lists.newArrayList();
            if (StringUtils.isEmpty(activityType)) {
                activityTypeList.add("党员大会");
                activityTypeList.add("党小组会");
                activityTypeList.add("支委会");
                activityTypeList.add("党课");
                activityTypeList.add("双联系双报到");
                activityTypeList.add("主题党日");
                activityTypeList.add("民主生活会");
                activityTypeList.add("组织生活会");
            } else {
                if (activityType.equals("党员大会")) {
                    activityTypeList.add("党员大会");
                }
                if (activityType.equals("党小组会")) {
                    activityTypeList.add("党小组会");
                }
                if (activityType.equals("支委会")) {
                    activityTypeList.add("支委会");
                }
                if (activityType.equals("党课")) {
                    activityTypeList.add("党课");
                }
                if (activityType.equals("双联系双报到")) {
                    activityTypeList.add("双联系双报到");
                }
                if (activityType.equals("主题党日")) {
                    activityTypeList.add("主题党日");
                }
                if (activityType.equals("民主生活会")) {
                    activityTypeList.add("民主生活会");
                }
                if (activityType.equals("组织生活会")) {
                    activityTypeList.add("组织生活会");
                }
            }

            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(partyOrganizationActivityService
                    .getMicroManagerPartyOrganizationActivityList
                            (search,
                                    partyOrganizationSerialNumber,
                                    activityTypeList,
                                    activityStatus,
                                    "支部",phone, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党建事务 党建活动 详情
     *
     * @param partyOrganizationActivitySerialNumber
     * @return
     */
    @Operation(summary = "小程序 党建事务 党建活动 详情")
    @Parameters({
            @Parameter(name = "partyOrganizationActivitySerialNumber", description = "主键"),
    })
    @GetMapping("/activity/detail")
    @ResponseBody
    public AjaxResult getMicroPartyOrganizationActivitiesStaticVoDetail(@RequestParam String partyOrganizationActivitySerialNumber) {
        try {
            return success(partyOrganizationActivityService.getMicroPartyOrganizationActivitiesStaticVoDetail(partyOrganizationActivitySerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党建事务 发布任务
     *
     * @return
     */
    @Operation(summary = "小程序 党建事务 发布任务")
    @Parameters({
            @Parameter(name = "type", description = "任务类型"),
            @Parameter(name = "phone", description = "用户id"),
            @Parameter(name = "search", description = "搜索"),
            @Parameter(name = "partyOrganizationSerialNumber", description = "党组织编号"),
            @Parameter(name = "status", description = "当前状态"),
            @Parameter(name = "pageNum", description = "当前页数"),
            @Parameter(name = "pageSize", description = "每页条数")
    })
    @GetMapping("/activity/getMicroManagerOrgTask")
    @ResponseBody
    public AjaxResult getMicroManagerOrgTask(
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String search,
            @RequestParam(required = false) String partyOrganizationSerialNumber,
            @RequestParam(required = false)  String status,
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }

            return success(partyOrganizationActivityService
                    .getMicroManagerOrgTask(
                            search,
                            type,
                            partyOrganizationSerialNumber,
                            phone,status,
                            pageNum, pageSize)
            );
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 党建事务 发布任务 帮扶详情
     *
     * @return
     */
    @Operation(summary = "小程序 党建事务 发布任务 帮扶详情")
    @Parameters({
            @Parameter(name = "type", description = "任务类型"),
            @Parameter(name = "id", description = "任务id")
    })
    @GetMapping("/activity/getMicroManagerOrgTaskDetail")
    @ResponseBody
    public AjaxResult getMicroOrgTaskDetail(
            @RequestParam String type,
            @RequestParam String id) {
        try {
            return success(partyOrganizationActivityService
                    .getMicroOrgTaskDetail(
                            type,
                            id)
            );
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
}
